package com.example.campuslink.ui.activity.ai

import android.os.Handler
import android.os.Looper
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.databinding.ActivityAboutUsBinding
import com.example.campuslink.ui.activity.BaseActivity
import com.example.campuslink.viewModel.MainViewModel

class AboutUsActivity : BaseActivity<ActivityAboutUsBinding, MainViewModel>(
    MainViewModel::class.java,
    R.layout.activity_about_us,
    BR.viewModel
) {
    private var handler: Handler? = null
    private var index1 = 0
    private var index2 = 0
    private var index3 = 0
    private var aiIntroduce = ""
    private var aiThink = ""
    private var aiAnswer = ""
    override fun initView() {
        Glide.with(this).load(R.drawable.ai_img)
            .error(R.drawable.ic_empty)
            .into(mViewDataBinding?.ivAiImg!!)
        initData()
        startTextAnimation()
    }

    private fun initData() {
        handler = Handler(Looper.getMainLooper())
        aiIntroduce = getString(R.string.ai_introduce)
        aiThink = getString(R.string.ai_think)
        aiAnswer = getString(R.string.ai_ask)

    }

    private fun startTextAnimation() {
        handler!!.postDelayed({
            if (index1 < aiIntroduce.length) {
                mViewDataBinding?.tvIntroduce?.append(aiIntroduce.get(index1).toString())
                index1++
                // 递归调用，继续显示下一个字符
                startTextAnimation()
            } else {
                startTextAnimation2()
            }
        }, 100) // 每隔 100 毫秒显示一个字符
    }

    private fun startTextAnimation2() {
        handler!!.postDelayed({
            if (index2 < aiThink.length) {
                mViewDataBinding?.tvThink?.append(aiThink.get(index2).toString())
                index2++
                // 递归调用，继续显示下一个字符
                startTextAnimation2()
            } else {
                startTextAnimation3()
            }
        }, 100) // 每隔 100 毫秒显示一个字符
    }

    private fun startTextAnimation3() {
        handler!!.postDelayed({
            if (index3 < aiAnswer.length) {
                mViewDataBinding?.tvAnswer?.append(aiAnswer.get(index3).toString())
                index3++
                // 递归调用，继续显示下一个字符
                startTextAnimation3()
            }
        }, 100) // 每隔 100 毫秒显示一个字符
    }

    override fun onDestroy() {
        super.onDestroy()
        handler?.removeCallbacksAndMessages(null)
    }
}